<template>
    <div class="container">
        <h2 class="orderTitle">我的订单</h2>
        <div class="orderList">
            <div v-for="order in orders" :key="order.id" class="orderItem">
                <div class="orderHeader">
                    <span class="orderId">订单号: {{ order.id }}</span>
                    <span class="orderDate">{{ order.date }}</span>
                </div>

                <div class="order-content">
                    <div class="order-info">
                        <div class="info-row">
                            <span>商品数量:</span>
                            <span>{{ order.quantity }}件</span>
                        </div>
                        <div class="info-row">
                            <span>总金额:</span>
                            <span class="price">{{ order.totalPrice !== null ? `¥${order.totalPrice}` : '待计算' }}</span>
                        </div>
                    </div>

                    <div class="order-actions">
                        <button class="action-btn detail-btn" @click="viewDetail(order.id)">查看详情</button>
                        <button class="action-btn cancel-btn" @click="cancelOrder(order.id)"
                            v-if="order.totalPrice !== null">取消订单</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';

const orders = ref([]);

onMounted(() => {
    // 从localStorage获取订单数据
    const storedOrders = localStorage.getItem('orders');
    if (storedOrders) {
        orders.value = JSON.parse(storedOrders);
    } else {
        orders.value = [];
    }
});

const viewDetail = (orderId) => {
    console.log('查看订单详情:', orderId);
    // 这里可以添加路由跳转逻辑
};

const cancelOrder = (orderId) => {
    console.log('取消订单:', orderId);
    // 删除localStorage中的订单数据,localStorage的filter方法，用于过滤出符合条件的订单数据
    orders.value = orders.value.filter(order => order.id !== orderId);
    localStorage.setItem('orders', JSON.stringify(orders.value));
};

</script>

<style scoped>
.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    font-family: 'Helvetica Neue', Arial, sans-serif;
    margin-bottom: 100px;
}

.orderTitle {
    text-align: center;
    color: #333;
    margin-bottom: 30px;
    font-size: 24px;
}

.orderList {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.orderItem {
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.orderItem:hover {
    transform: translateY(-3px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
}

.orderHeader {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    background-color: #f8f9fa;
    border-bottom: 1px solid #eee;
}

.orderId {
    font-weight: 600;
    color: #555;
}

.orderDate {
    color: #777;
    font-size: 14px;
}

.order-content {
    padding: 20px;
}

.order-info {
    margin-bottom: 15px;
}

.info-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    color: #666;
}

.info-row span:first-child {
    font-weight: 500;
}

.price {
    color: #e74c3c;
    font-weight: 600;
}

.order-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.action-btn {
    padding: 8px 16px;
    border-radius: 4px;
    border: none;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s ease;
}

.detail-btn {
    background-color: #3498db;
    color: white;
}

.detail-btn:hover {
    background-color: #2980b9;
}

.cancel-btn {
    background-color: #e74c3c;
    color: white;
}

.cancel-btn:hover {
    background-color: #c0392b;
}

@media (max-width: 600px) {
    .orderHeader {
        flex-direction: column;
        align-items: flex-start;
        gap: 5px;
    }

    .order-actions {
        justify-content: space-between;
    }

    .action-btn {
        width: 48%;
        text-align: center;
    }
}
</style>
